﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;

public partial class User_FindBackPassword : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {

    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string JobId = TextBox1.Text.Trim();
            string Email = TextBox2.Text.Trim();

            var result = (from r in db.UserInfo
                          where r.JobId == JobId && r.Email == Email
                          select r).FirstOrDefault();

            if (result == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "failMessage", "<script> alert('工号或邮箱信息有误，找回密码失败！');</script>");
            }
            else
            {
                string Password = result.Password;

                using(var smtpClient=new SmtpClient())
                {
                    smtpClient.EnableSsl = true;
                    smtpClient.Host = "smtp.qq.com";
                    smtpClient.Port = 587;
                    smtpClient.Credentials = new NetworkCredential("775026642", "lfkzyidcmlsibfic");
                    string body = "您的工号：" + JobId + "\n您的密码：" + Password;

                    MailMessage mailMessage = new MailMessage(
                        "775026642@qq.com",
                        Email,
                        "软件外包管理系统 | 用户密码找回",
                        body);

                    smtpClient.Send(mailMessage);

                }



                Response.Redirect("Login.aspx?JobId="+JobId+"&Type=FindBackPassword");
            }


        }
    }
}